package com.longteng.lesson.controller;

import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator
 * DATE : 2018/4/25
 * TIME : 23:24
 * PROJECT : java-lesson
 * PACKAGE : com.longteng.lesson
 *
 * @author <a href="mailto:celeskyking@163.com">Administrator</a>
 */
@Controller
@RequestMapping(value = "/c")
public class TestController {

    @GetMapping(value = "/hello")
    public String helloworld() {
        return "helloworld";
    }

    @GetMapping(value = "/{name}/{email}")
    public @ResponseBody
    String restfulTest(@PathVariable String name, @PathVariable String email, String a) {
        return name + ":" + email + ":" + a;
    }

//    @GetMapping(value = "/name/email")
//    public @ResponseBody
//    String restfulTest1(HttpServletRequest request, HttpSession session,
//                        String name, Integer age) {
//        //整形 int 1232112
//        Cookie[] cookies =request.getCookies();
//        HttpSession sessionObj = request.getSession();
//        return name + ":" + age;
//    }
    @RequestMapping("/setCookie")
    public void setCookie(HttpServletResponse response) {
        Cookie cookie = new Cookie("xiaomao", "chiyu");
        cookie.setMaxAge(60*60);
        response.addCookie(cookie);
    }
    @RequestMapping("/removeCookie")
    public void clearCookie(HttpServletRequest request, HttpServletResponse response, String name) {
        Cookie[] cookies = request.getCookies();
        for(Cookie cookie:cookies){
            String cookieName =cookie.getName();
            if(cookieName.equalsIgnoreCase(name)){
                cookie.setMaxAge(0);
                response.addCookie(cookie);
                break;
            }
        }
    }

    @GetMapping(value = "/postTest")
    public @ResponseBody
    String postTest() {
        return "hi";
    }

    @RequestMapping(value = "/requestMappingTest")
    public String requestMappingTest() {
        return "requestMappingTest";
    }

    @RequestMapping(value = "/requestBodyTest")
    public @ResponseBody
    Map requestBodyTest(@RequestBody Map map, HttpServletRequest request, String name, @RequestHeader(value = "Content-Type") String header) {
        System.out.println(header);
//        String body = "";
//        try {
//            BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
//            body = IOUtils.toString(reader);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        return map;
    }

    @RequestMapping(value = "/getMap")
    public Map getMap() {
        Map map = new HashMap();
        map.put("name", "taki");
        return map;
    }

    //1.当requestParam里面不设置任何属性的时候，请求的url参数必须包含 age的参数
    //2.当required设置成false的时候可以不用必须包含
    //3.当有默认值的时候即使required设置为true也可以请求,从默认值里面获取入参
    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    public @ResponseBody
    List getList(@RequestParam(value = "name") String l, String age) {
        System.out.println("userName:" + l + ",age:" + age);
        List list = new ArrayList();
        Map map = new HashMap();
        map.put("name", "taki");
        list.add(map);
        return list;
    }

}
